Skip to content

fix: handle undefined model in normalizeModelName to prevent crash on rate limits#7

Open
wende wants to merge 1 commit intoatalovesyou:mainfrom
wende:fix-normalizeModelName-undefined
Open

fix: handle undefined model in normalizeModelName to prevent crash on rate limits#7
wende wants to merge 1 commit intoatalovesyou:mainfrom
wende:fix-normalizeModelName-undefined

Conversation

@wende
Copy link

@wende wende commented Feb 6, 2026

Description

This PR fixes a crash that occurs when normalizeModelName is called with undefined.

Bug

When Claude CLI returns a rate limit response or other error condition, result.modelUsage can be an empty object {}. The current code:

const modelName = result.modelUsage
  ? Object.keys(result.modelUsage)[0]
  : "claude-sonnet-4";

Returns undefined when modelUsage is {}, because Object.keys({})[0] is undefined.

This causes:

TypeError: Cannot read properties of undefined (reading 'includes')
    at normalizeModelName (cli-to-openai.js:88:15)

Fix

  1. Updated normalizeModelName to accept string | undefined
  2. Added null/undefined guard that returns "claude-sonnet-4" as default
  3. This handles all edge cases where model might not be available

Testing

  • Proxy no longer crashes when rate limit is hit
  • Returns sensible default model name in error responses
  • Normal model name normalization still works correctly

Fixes the issue where the server crashes with:

[Subprocess] Process closed with code: 1
file:///.../cli-to-openai.js:88
    if (model.includes("opus"))
              ^
TypeError: Cannot read properties of undefined (reading 'includes')

The normalizeModelName function would crash with:
TypeError: Cannot read properties of undefined (reading 'includes')

This happened when:
1. result.modelUsage exists but is an empty object {}
2. Object.keys(result.modelUsage)[0] returns undefined
3. normalizeModelName(undefined) is called

This commonly occurs when Claude CLI returns rate limit responses
or other error conditions where modelUsage is empty.

The fix adds a null/undefined check that returns a sensible default
(claude-sonnet-4) when the model is not available.

Also updated the type signature to reflect that model can be undefined.
jamshehan added a commit to jamshehan/claude-max-api-proxy that referenced this pull request Feb 8, 2026
Incorporates fixes from PR atalovesyou#5 (coraAIbot) and PR atalovesyou#7 (wende):

- Add --dangerously-skip-permissions flag for service/proxy usage
  Enables full system access when running as API proxy (file I/O, network, PM2, etc.)

- Add system prompt support via --append-system-prompt
  Handles both short system prompts (CLI flag) and long prompts (stdin)
  Prevents ENAMETOOLONG on Windows by using 8000 char threshold

- Fix content array extraction (PR atalovesyou#5 + our implementation)
  Handle OpenAI content format: string | ContentPart[]
  Extract text from content blocks correctly

- Fix rate limit crashes (PR atalovesyou#7)
  Guard against undefined model names when rate limits are hit

- Add debug logging with DEBUG_SUBPROCESS env var
  Helps diagnose prompt issues and subprocess behavior

- Add support for 'developer' role in OpenAI spec

- Keep stdin approach for main prompts (avoid ENAMETOOLONG)

All changes tested and verified with OpenClaw integration.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
smartchainark pushed a commit to smartchainark/claude-max-api-proxy that referenced this pull request Feb 10, 2026
When Claude CLI returns a rate limit response, modelUsage can be an
empty object causing Object.keys({})[0] to return undefined, which
crashes normalizeModelName.

Credit: PR atalovesyou#7 by @wende

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
bwiedmann added a commit to bwiedmann/claude-max-api-proxy that referenced this pull request Feb 15, 2026
…lizeModelName to prevent crash on rate limits
wende added a commit to wende/claude-max-api-proxy that referenced this pull request Feb 18, 2026
Triaged all 14 open PRs from atalovesyou/claude-max-api-proxy,
implemented the valuable fixes, and added end-to-end test coverage.

Changes:
- Fix normalizeModelName crash on undefined model (atalovesyou#7 regression)
- Pass prompt via stdin instead of CLI arg to avoid E2BIG (atalovesyou#12)
- Increase subprocess timeout from 5 to 15 minutes (atalovesyou#20)
- Add Claude 4.5/4.6 model IDs and claude-max/ prefix (atalovesyou#10, atalovesyou#20)
- Include usage data in final streaming SSE chunk (atalovesyou#16)
- Wrap subprocess logging with DEBUG_SUBPROCESS env check (atalovesyou#5, atalovesyou#16)
- Strip CLAUDECODE env var from subprocesses (own fix)
- Add e2e test suite (7 tests covering health, models, completions)
wende added a commit to wende/claude-max-api-proxy that referenced this pull request Feb 18, 2026
Triaged all 14 open PRs from atalovesyou/claude-max-api-proxy,
implemented the valuable fixes, and added end-to-end test coverage.

Changes:
- Fix normalizeModelName crash on undefined model (atalovesyou#7 regression)
- Pass prompt via stdin instead of CLI arg to avoid E2BIG (atalovesyou#12)
- Increase subprocess timeout from 5 to 15 minutes (atalovesyou#20)
- Add Claude 4.5/4.6 model IDs and claude-max/ prefix (atalovesyou#10, atalovesyou#20)
- Include usage data in final streaming SSE chunk (atalovesyou#16)
- Wrap subprocess logging with DEBUG_SUBPROCESS env check (atalovesyou#5, atalovesyou#16)
- Strip CLAUDECODE env var from subprocesses (own fix)
- Add e2e test suite (7 tests covering health, models, completions)

Co-Authored-By: kevinfealey <10552286+kevinfealey@users.noreply.github.com>
Co-Authored-By: Max <257223904+Max-shipper@users.noreply.github.com>
Co-Authored-By: James Hansen <1359077+jamshehan@users.noreply.github.com>
Co-Authored-By: bitking <213560776+smartchainark@users.noreply.github.com>
Co-Authored-By: Alex Rudloff's AI Agents <258647843+alexrudloffBot@users.noreply.github.com>
Grivn added a commit to mnemon-dev/claude-max-api-proxy that referenced this pull request Feb 20, 2026
Fixes applied (from upstream PRs atalovesyou#7, atalovesyou#11, atalovesyou#12, atalovesyou#13, atalovesyou#17, atalovesyou#20):

- fix: normalizeModelName crash on undefined model (issue atalovesyou#21)
- fix: [object Object] serialization for array content parts
- fix: E2BIG error by passing prompt via stdin instead of CLI arg
- fix: ensureString on result.result to prevent non-string output
- feat: CLAUDE_DANGEROUSLY_SKIP_PERMISSIONS env var for headless mode
- feat: OPENCLAW_PROXY=1 env injection for hook isolation
- feat: model aliases for claude-*-4-5, claude-*-4-6 generations
- feat: claude-proxy/ provider prefix support
- feat: developer role support in message conversion
- perf: increase subprocess timeout from 5min to 15min

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant